package com.zprmb.udzt;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zprmb.udzt.adapter.BankAdapter;
import com.zprmb.udzt.bean.BankBean;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

public class Standby_bank extends BaseActivity implements OnClickListener{
	private RelativeLayout rl_stand_bank;
	private ListView lv;
	private BankAdapter adapter;
	private List<BankBean> list=new ArrayList<BankBean>();
	private ImageView img_back;
	private int who;
	private TextView tv_title;
	private String bankNo="1";
	private RelativeLayout rl_back;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stand_bank);
		 BbtApplication.getInstance().addActvity(this);
		lv=(ListView)findViewById(R.id.lv);
		img_back=(ImageView)findViewById(R.id.img_back);
		tv_title=(TextView)findViewById(R.id.title);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		rl_back.setOnClickListener(this);
		img_back.setOnClickListener(this);
		who=getIntent().getIntExtra("bind", 0);
		if(who==1){
			tv_title.setText("选择银行");
		}else{
			tv_title.setText("支持银行");
		}
		initdata();
		bankNo=getIntent().getStringExtra("bankNo");
		if(getIntent().getBooleanExtra("bank", false)){
//			lv.setEnabled(false);
		}
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.putExtra("bank",list.get(position).getBankNameEn());
				intent.putExtra("img",list.get(position).getLogoUrl());
				intent.putExtra("name",list.get(position).getBankName());
				 setResult(1, intent);  
				 finish();
			}
		});
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	private void initdata() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("bank", jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					
					Gson gson=new Gson();
					if (!jsonObject.optString("data").equals("null")) {
						list=gson.fromJson(jsonObject.optString("data"), new TypeToken<ArrayList<BankBean>>() {
						}.getType());
						
					}					
					adapter=new BankAdapter(list, Standby_bank.this,bankNo);
					
					lv.setAdapter(adapter); 
					adapter.notifyDataSetChanged();
				}else{
         if (jsonObject.optString("comment").equals("please login")) {
						
					
						if(getShare().getString("mobile", "").equals("")){
							Intent intent = new Intent(Standby_bank.this,Log_or_RegisActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}else{
							
							Intent intent = new Intent(Standby_bank.this,LoginActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}
					}else{
						Toast.makeText(Standby_bank.this, jsonObject.optString("comment"), 2000).show();
						
					}
				}
			
				
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setRequestMethod(0);
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setShow(true);
		volleryUtil.execute(Standby_bank.this, Constant.URL+"/api/v5/account/bankcard/supports", "");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.img_back:
			finish();
			break;
		case R.id.rl_back:
			finish();
			break;
		}
		
	}

}
